package com.hotacorp.opencloud.flowservice.controller;

import java.util.Date;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hotacorp.opencloud.common.commdata.Result;
import com.hotacorp.opencloud.common.log.annotation.LogAnnotation;
import com.hotacorp.opencloud.common.mybatisplus.PageData;
import com.hotacorp.opencloud.common.resourcecommon.validator.group.AddGroup;
import com.hotacorp.opencloud.common.resourcecommon.validator.group.UpdateGroup;
import com.hotacorp.opencloud.flowservice.entity.LeaveRequestEntity;
import com.hotacorp.opencloud.flowservice.service.LeaveRequestService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;



/**
 * 请假申请
 *
 * @author lwg
 * @email liwugang@139.com
 * @date 2020-01-11 12:17:00
 */
@RestController
@Api(tags = "请假")
public class LeaveRequestController extends AbstractController {
    @Autowired
    private LeaveRequestService leaveRequestService;

    /**
     * 列表
     */
    @GetMapping(value = "/leaverequest/list")
    @PreAuthorize("hasAuthority('flowservice:leaverequest:list')")
	@ApiOperation(value = "查询请假")
    @LogAnnotation(module = "请假")
    public Result<?> list(@RequestParam Map<String, Object> params){
    	params.put("tenantid", getTenantid());
    	params.put("username", getUsername());
        PageData page = leaveRequestService.queryPage(params);

        return Result.succeed("succeed", page);
    }

    /**
     * 信息
     */
    @GetMapping(value = "/leaverequest/info/{id}")
    @PreAuthorize("hasAuthority('flowservice:leaverequest:info')")
	@ApiOperation(value = "请假详情")
    @LogAnnotation(module = "请假")
    public Result<?> info(@PathVariable("id") Long id){
		LeaveRequestEntity leaveRequest = leaveRequestService.getById(id);

        return Result.succeed("succeed", leaveRequest);
    }

    /**
     * 申请
     */
    @PostMapping(value = "/leaverequest/request")
    @PreAuthorize("hasAuthority('flowservice:leaverequest:request')")
	@ApiOperation(value = "请假申请")
    @LogAnnotation(module = "请假")
    public Result<?> request(@RequestBody @Validated(AddGroup.class) LeaveRequestEntity leaveRequest){
    	leaveRequest.setCreatetime(new Date());
    	leaveRequest.setUpdatetime(new Date());
    	leaveRequest.setTenantid(getTenantid());
    	leaveRequest.setUsername(getUsername());
		Long ret = leaveRequestService.insert(leaveRequest);

        return Result.succeed("", ret);
    }

    /**
     * 重新申请
     */
    @PostMapping(value = "/leaverequest/update")
    @PreAuthorize("hasAuthority('flowservice:leaverequest:request')")
	@ApiOperation(value = "请假重新申请")
    @LogAnnotation(module = "请假")
    public Result<?> update(@RequestBody @Validated(UpdateGroup.class) LeaveRequestEntity leaveRequest){
    	leaveRequest.setUpdatetime(new Date());
    	leaveRequest.setTenantid(getTenantid());
    	leaveRequest.setUsername(getUsername());
		leaveRequestService.updateById(leaveRequest);

        return Result.succeed();
    }

    /**
     * 设置流程实例ID
     */
    @GetMapping(value = "/leaverequest/setproc")
    @PreAuthorize("hasAuthority('flowservice:leaverequest:request')")
	@ApiOperation(value = "请假设置流程ID")
    @LogAnnotation(module = "请假")
    public Result<?> setproc(@RequestParam(required = true) Long leaveid, @RequestParam(required = true) String proid){
    	boolean ret = leaveRequestService.setProcessInstanceId(getTenantid(), getUsername(), leaveid, proid);
    	if (ret) {
    		return Result.succeed();
    	} else {
    		return Result.failed("数据传输错误");
    	}
    }

}
